<!-- 💥 一次性加载 LayoutComponents -->
<script setup lang="ts" name="layout">
import type { Component } from 'vue'
import type { LayoutType } from '@/constants'
import { useGlobalStore } from '@/stores'

const Settings = defineAsyncComponent(() => import('./components/Settings/index.vue'))
const SettingIcon = defineAsyncComponent(() => import('./components/SettingIcon/index.vue'))
const LayoutClassic = defineAsyncComponent(() => import('./LayoutClassic/index.vue'))
const LayoutColumns = defineAsyncComponent(() => import('./LayoutColumns/index.vue'))

const LayoutComponents: Record<LayoutType, Component> = {
  vertical: LayoutClassic,
  columns: LayoutColumns,
}
const globalStore = useGlobalStore()
const { layout, settingsVisible } = storeToRefs(globalStore)
// 监听布局变化，在 body 上添加相对应的 layout class
watch(
  () => layout.value,
  () => {
    const body = document.body as HTMLElement
    body.setAttribute('class', layout.value)
  },
  { immediate: true },
)
</script>

<template>
  <component :is="LayoutComponents[layout]" />
  <SettingIcon />
  <Settings v-model="settingsVisible" />
</template>

<style scoped lang="scss">
.layout {
  min-width: 600px;
}
</style>
